home *** CD-ROM | disk | FTP | other *** search
- unit ClipHelp;
-
- interface
-
- uses
- Classes;
-
- const
- CF_COMPONENTS: Word = 0;
-
- procedure ClipBoardGetComponents(Owner, Parent: TComponent);
- procedure ClipBoardSetComponents(Components: array of TComponent);
-
- implementation
-
- uses
- ClipBrd, WinTypes, WinProcs, SysUtils, Controls;
-
- procedure ClipBoardSetComponents(Components: array of TComponent);
- var
- ClipStream: TMemoryStream;
- Loop: Integer;
- Data: THandle;
- DataPtr: Pointer;
- begin
- ClipStream := TMemoryStream.Create;
- try
- for Loop := Low(Components) to High(Components) do
- ClipStream.WriteComponent(Components[Loop]);
- { Reset stream pointer to beginning }
- ClipStream.Position := 0;
- { Allocate memory block to give to clipboard }
- Data := GlobalAlloc(GMEM_MOVEABLE and GMEM_DDESHARE, ClipStream.Size);
- if Data = 0 then
- OutOfMemoryError;
- { Lock it for writing }
- DataPtr := GlobalLock(Data);
- try
- ClipStream.Read(DataPtr^, ClipStream.Size);
- finally
- { Unlock it }
- GlobalUnlock(Data)
- end;
- { Clipboard takes ownership of memory block, so we can forget it }
- ClipBoard.SetAsHandle(CF_COMPONENTS, Data)
- finally
- ClipStream.Free
- end;
- end;
-
- procedure ClipBoardGetComponents(Owner, Parent: TComponent);
- var
- Data: THandle;
- DataPtr: Pointer;
- ClipStream: TMemoryStream;
- Comp: TComponent;
- const
- FilerSignature: array[1..4] of Char = 'TPF0';
- begin
- ClipBoard.Open;
- try
- Data := GetClipboardData(CF_COMPONENTS);
- if Data = 0 then
- Exit;
- DataPtr := GlobalLock(Data);
- if DataPtr = nil then
- Exit;
- try
- ClipStream := TMemoryStream.Create;
- try
- ClipStream.WriteBuffer(DataPtr^, GlobalSize(Data));
- ClipStream.Position := 0;
- repeat
- { Check for VCL stream signature before proceeding }
- if PLongint(Longint(ClipStream.Memory) + ClipStream.Position)^ <>
- Longint(FilerSignature) then
- Exit;
- Comp := ClipStream.ReadComponent(nil);
- if Comp is TControl then
- TControl(Comp).Parent := Parent as TWinControl;
- try
- Owner.InsertComponent(Comp)
- except
- Comp.Free;
- raise
- end
- { We will probably leave thanks to the signature }
- { before check this condition is met as Windows }
- { memory is rounded up in size, so there will be slack }
- until ClipStream.Position = ClipStream.Size
- finally
- ClipStream.Free
- end
- finally
- GlobalUnlock(Data)
- end
- finally
- ClipBoard.Close
- end
- end;
-
- initialization
- CF_COMPONENTS := RegisterClipboardFormat('Delphi Components');
- end.
-